# -*- coding:utf8 -*-
from __future__ import print_function
import os
import sys
import argparse
from flask_cors import *
from flask_utility import RestApiProcess, Flask
from logger_utility import getLogger, LoggerConfig, DEBUG
from config import LOGS_PATH, LOG_FILE_BASE

# 先配置日志，后面再执行本项目相关的代码
logger = getLogger('Server')
logger_config = LoggerConfig(os.path.join(LOGS_PATH, LOG_FILE_BASE))
log_to_conso = os.environ.get('LogTerm')
logger_config.config(to_screen=log_to_conso, level=DEBUG)


def check_sensitive():
    pass


def init():
    pass


app = Flask(__name__)
CORS(app, supports_credentials=True)
rest_api_process = RestApiProcess(logger)

init()


@app.route('/', methods=['POST', 'GET'])
@rest_api_process.wrap
def process(in_data):
    """
    实际查找敏感词的函数
    :param in_data: 
    :return: 
    """
    content = in_data.get('content')
    ret_dict = check_sensitive()
    return ret_dict


def main(argv=None):
    """
    启动脚本
    :param argv:参数，如果未制定则使用sys.argv，可以通过赋参用于测试
    :return: 0或-1
    """
    if argv is None:
        argv = sys.argv

    parser = argparse.ArgumentParser(description='')
    parser.add_argument('-p', '--port', type=int, default=10065, help='设置端口')
    args = parser.parse_args(argv[1:])

    app.run(debug=False, host='0.0.0.0', port=args.port, threaded=True)
    return 0


def create_command():
    """
    创建命令行
    :return:
    """
    from code_template.gunicorn_flask import create_cmd
    create_cmd(__file__, port=10066, server='local')


if __name__ == '__main__':
    create_command()
    sys.exit(main())

